package xyz.archknight.app.loginput.dictionary.gen.service

import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

/**
 * 内训数据库转储序列化工具
 *
 * @author midnite
 * create at 2020-06-19 10:07 下午
 */
fun serialize(obj: Any): ByteArray {
    ByteArrayOutputStream().use { baos ->
        ObjectOutputStream(baos).use { oos ->
            oos.writeObject(obj)
            return baos.toByteArray()
        }
    }
}

@Suppress("UNCHECKED_CAST")
fun <T> unserialize(data: ByteArray): T {
    ByteArrayInputStream(data).use { bais ->
        ObjectInputStream(bais).use { ois ->
            return ois.readObject() as T
        }
    }
}